﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cyberwolf.Modules.PWCAPAdmin.Components;
using DotNetNuke.Common;

namespace Cyberwolf.Modules.PWCAPAdmin.UserControls
{
    public partial class MainMenu : System.Web.UI.UserControl
    {
        private PWCAPAdminBaseClass _Container;
		public PWCAPAdminBaseClass Container
		{
			get { return _Container; }
			set { _Container = value; }
		}

        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterProcessor("Organization", "Update Profile", "UpdateProfile", "OrgProfileUpd");
            RegisterProcessor("Organization", "Update Parties", "UpdateParties", "SecurityAdminOrg");
            RegisterProcessor("Organization", "Update Organization", "UpdateOrganization", "OrgSecurityAll");

            RegisterProcessor("Development", "Test Process Xml", "TestProcessXml", "Development");
            RegisterProcessor("Development", "Database Scripts", "DatabaseScripts", "Development");

            RegisterProcessor("Configuration", "Cryptography", "Cryptography", "AppConfigureAdmin");
            RegisterProcessor("Configuration", "Connections", "Connections", "DBConnectionsAdmin");

            RegisterProcessor("System", "PWAP Info", "PWAPInfo", "Application");

            Page.ClientScript.RegisterStartupScript(this.GetType(), "PWCAPAdminMenu", "PWCAPAdminMenu();", true);
        }

        private void RegisterProcessor(string MenuCategory, string MenuItem, string ControlKey, string Responsibility)
        {
            if (PWSecurityToken.Current == null) return;
            if (!PWSecurityToken.Current.PartyRights.Contains(Responsibility))
            {
                if (ControlKey.Equals(Container.ModuleConfiguration.ModuleControl.ControlKey))
                {
                    Response.Redirect(Globals.NavigateURL(Container.TabId, "Login", "mid=" + Container.ModuleId, Container.GetActiveContainerAndSkin() + "&ReturnUrl=" + System.Web.HttpUtility.UrlEncode(this.Request.RawUrl)));
                }

                return;
            }

            var module = phModules.FindControl<HyperLink>(MenuCategory);
            if (module == null)
            {
                module = new HyperLink();
                module.ID = MenuCategory;
                module.Text = MenuCategory;
                phModules.Controls.AddAt(0, module);
            }
            var moduleMenu = this.FindControl<Panel>(MenuCategory);
            if (moduleMenu == null)
            {
                moduleMenu = new Panel();
                moduleMenu.ID = MenuCategory;
                moduleMenu.Controls.Add(new LiteralControl("<br class=\"clearfix\" />"));
                this.Controls.Add(moduleMenu);
            }
            var moduleMenuItem = new HyperLink();
            moduleMenuItem.ID = MenuItem;
            moduleMenuItem.Text = MenuItem;
            moduleMenuItem.NavigateUrl = Globals.NavigateURL(Container.TabId, ControlKey, "mid=" + Container.ModuleId, Container.GetActiveContainerAndSkin());
            moduleMenu.Controls.AddAt(1, moduleMenuItem);
        }
    }
}